package com.candy.candymessage.config;

import com.candy.candymessage.business.entity.MailConfig;
import com.candy.candymessage.business.service.IMailConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author suYanLong
 * @date 2022年10月26日 16:57
 */
@Slf4j
@Component
@AllArgsConstructor
public class MailSenderConfig {

    private final MailConfigs mailConfig;

    final List<JavaMailSenderImpl> senderList;

    static int num = 0;

    private final IMailConfigService iMailConfigService;

    /**
     * 初始化 sender
     */
    @PostConstruct
    public void buildMailSender() {
        clear();

        List<MailConfig> list = iMailConfigService.list();

        List<MailConfigs.MailProperties> mailConfigs = mailConfig.getConfigs();
        log.info("初始化 配置中 mailSender");
        mailConfigs.forEach(mailProperties -> {
            // 邮件发送者
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setDefaultEncoding(mailProperties.getDefaultEncoding());
            javaMailSender.setHost(mailProperties.getHost());
            javaMailSender.setPort(mailProperties.getPort());
            javaMailSender.setProtocol(mailProperties.getProtocol());
            javaMailSender.setUsername(mailProperties.getUsername());
            javaMailSender.setPassword(mailProperties.getPassword());
            // 添加数据
            senderList.add(javaMailSender);
        });

        log.info("初始化 数据库中 mailSender");
        list.forEach(mailProperties -> {
            // 邮件发送者
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setDefaultEncoding(mailProperties.getDefaultEncoding());
            javaMailSender.setHost(mailProperties.getHost());
            javaMailSender.setPort(mailProperties.getPort());
            javaMailSender.setProtocol(mailProperties.getProtocol());
            javaMailSender.setUsername(mailProperties.getUserName());
            javaMailSender.setPassword(mailProperties.getPassword());
            // 添加数据
            senderList.add(javaMailSender);
        });
    }

    /**
     * 获取MailSender * @return CustomMailSender
     */
    public JavaMailSenderImpl getSender() {

        if (senderList.isEmpty()) {
            buildMailSender();
        }
        if (senderList.isEmpty()) {
            return null;
        }
        if (num >= senderList.size()) {
            num = 1;
        } else {
            num++;
        }
        int i = num % (senderList.size());
        log.info("num = {}, index = {}", num, i);
        // 随机返回一个JavaMailSender
        return senderList.get(i);
    }

    public List<JavaMailSenderImpl> getAllSender() {
        return senderList;
    }

    /**
     * 清理 sender
     */
    public void clear() {
        senderList.clear();
    }

}
